package com.hn.sanya.demo.wx.server;

import com.hn.sanya.demo.wx.util.XmlToMapUtil;
import com.hn.sanya.demo.wx.vo.message.ImageMessage;
import com.hn.sanya.demo.wx.vo.message.TextMessage;
import com.hn.sanya.demo.wx.vo.message.VideoMessage;
import com.hn.sanya.demo.wx.vo.message.VoiceMessage;
import org.dom4j.DocumentException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 功能描述:处理文本的业务逻辑层
 * @param:
 * @return:
 * @auther: lxh
 * @date: 2019/2/1
 */
public class MessageServer {

    /**
     * 功能描述: 接收消息与响应消息
     * @param:
     * @return:
     * @auther: lxh
     * @date: 2019/1/30
     */
    public static  void getResponseXml(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {

        //1.接收消息并转化成map集合
        Map<String,String> requestMap= XmlToMapUtil.xmlToMap(request);

        //2.封装数据
        String xml=null;
        String msgType=requestMap.get("MsgType");
        switch(msgType){
            case "text":  //文本消息
                xml= XmlToMapUtil.MapToXml(new TextMessage(requestMap));
                break;
            case "image":  //图片消息
                xml=XmlToMapUtil.MapToXml(new ImageMessage(requestMap));
                break;
            case "voice":   //语音消息
                xml=XmlToMapUtil.MapToXml(new VoiceMessage(requestMap));
                break;
            case "video":   //视频消息
                xml=XmlToMapUtil.MapToXml(new VideoMessage(requestMap));
                break;
            case "shortvideo": //小视频消息
                xml=XmlToMapUtil.MapToXml(new TextMessage(requestMap));
                break;
            case "location":    //地理位置消息
                xml=XmlToMapUtil.MapToXml(new TextMessage(requestMap));
                break;
            case "link":    //链接消息
                xml=XmlToMapUtil.MapToXml(new TextMessage(requestMap));
                break;

            default:
                break;
        }

        //3.返回响应消息
        response.getWriter().write(xml);

    }
}
